package
{
	import flash.display.Sprite;
	import flash.geom.Rectangle;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;

	public class BinaryTreeNode
	{
		public var depth:int;
		public var parent:BinaryTreeNode;
		public var left:BinaryTreeNode;
		public var right:BinaryTreeNode;
		
		public var data:BinaryTreeNodeData;
		
		public function BinaryTreeNode(data:BinaryTreeNodeData = null, depth:int = 0)
		{
			this.data = data;
			this.depth = depth;
		}
		
		public function isNull():Boolean{
			return (data == null);
		}
		
		public function getHeight(depth:int = 0):int{
			if(left == null){
				return depth;
			} else {
				return left.getHeight(depth + 1);
			}
		}
		
		public function getDrawing():Sprite{
			var image:Sprite = new Sprite();
			image.graphics.lineStyle(1);
			image.graphics.drawRect(0,0,20,20);
			image.x = - image.width/2;
			
			var tf:TextField = new TextField();
			if(data != null){
				if(data.value == int.MIN_VALUE){
					tf.text = '-inf';
				}
				else{
					tf.text = String(data.value);
				}
			}
			else{
				tf.text = 'null';
			}
			tf.autoSize = TextFieldAutoSize.LEFT;
			image.addChild(tf);
			
			if(left){
				var leftImage:Sprite = left.getDrawing();
				
				leftImage.x = -leftImage.width/2;
				leftImage.y = 20;
				image.addChild(leftImage);
			}
			
			if(right){
				var rightImage:Sprite = right.getDrawing();
				
				rightImage.y = 20;
				rightImage.x = rightImage.width;
				image.addChild(rightImage);
			}
			
			return image;
		}
		
	}
}